<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 2018/6/21
 * Time: 10:05
 */

namespace app\common\model;


use think\Model;

class Courses extends Model
{
    //获取精品课程
    public function getFineCourse(){
        $fins = $this->alias("c")->join("xm_author a","c.author_id=a.id")->where(self::condition(1))->order("c.sort desc")->field(self::fields())->select();
        return $fins;
    }

    //获取推荐课程
    public function getGroomCourse(){
        $grooms = $this->alias("c")->join("xm_author a","c.author_id=a.id")->where(self::condition(2))->order("c.sort desc")->field(self::fields())->select();

        return $grooms;
    }




    //根据ID获取课程详情
    public function getCourseById($id){
        if(!$id && !is_numeric($id)){
            exception("传递的ID不合法");
        }
        //查询条件
        $where = array(
            'c.id'=>$id,
            'c.status' => 1
        );

        //获取传递过来ID的课程详情
        $course = $this->alias("c")->join("xm_author a","c.author_id=a.id")->where($where)->find();
        //获取大咖推荐的图片和音频
        $audio = model("big_shot")->where("course_id=".$id)->field("audio,img")->select();

        //修改数据键名
        $data = [
            'AuthorDesc' => '',   //作者描述
            'AuthorName' => $course['name'],   //作者名
            'BuyCount' => $course['num']*3,   //购买数量
            'Desc' => '',    //课程描述
            'Details' => $course['details'],  //课程详情
            'Explain' => null,
            'GroupPrice' => $course['g_price'],  //团购价格
            'Id' => $course['id'],   //主键
            'ImgUrl' => $course['cover'],   //封面图
            'ImgsDesc' => [],               //详情头部轮播
            'IsBuy' => false,
            'Mode' => '',
            'Outline' => $course['outline'],         //课程大纲
            'Path5'=>'',
            'Price' => $course['price'],       //价格
            'ShareImgDesc' => '',             //分享图片
            'ShareTemplateId' => '',          //分享模版
            'Title' => $course['title'],      //标题
            'TotalHours' => $course['total_hours'],      //课时
            'UpdateHours' => '',     //已更新课时
            'Whenlong' => $course['whenlong'],          //时长
            'Poster' => $course['poster']

        ];

        //拼接大咖推荐的数据
        for($i = 0;$i<=2;$i++){
            for($j =1;$j<=3;$j++){
                $data['Audio'.$j.'Desc'] = $audio[$i]['audio'];
                $data['Path'.$j.'Desc'] = $audio[$i]['img'];
            }
        }

        return $data;
    }


    //我的课程（立即评论）
    public function getCoursesById($id){
        if(!$id && !is_numeric($id)){
            exception("传递的ID不合法");
        }
        $where = array(
            'c.id' => $id,
        );

        return $this->alias("c")->join("xm_author a","c.author_id=a.id")->where($where)->field("c.id as CourseId,c.title as Title,a.name,c.img_url,c.price,c.g_price,c.total_hours,c.update_hours,c.whenlong")->find();
    }







    //查询条件封装
    public static function condition($category){
        $data = array(
            'c.category' => $category,
            'c.status' => 1,
        );
        return $data;
    }

    //获取字段封装
    public static function fields(){
        return [
            'c.id',
            'c.g_price',
            'c.price',
            'c.total_hours',
            'c.whenlong',
            'c.title',
            'c.img_url',
            'a.name'
        ];
    }

}